from django.db import models

# Create your models here.


# 学生表
class student(models.Model):
    stu_num = models.BigIntegerField(primary_key=True)  # 定义长整型学号，并设置为主键
    stu_pwd = models.CharField(max_length=32)  # 定义字符串，设置最大长度为32
    stu_name = models.CharField(max_length=32)
    stu_mingzu = models.CharField(max_length=32)
    stu_sex = models.CharField(max_length=32)
    stu_zhuanye = models.CharField(max_length=32)
    stu_jiguan = models.CharField(max_length=32)
    stu_birth = models.DateField()  # 定义出生日期
    str_pic = models.ImageField(upload_to='media')  # 定义头像，上传目录为media

    class Meta:
        verbose_name_plural = '学生表'

    def __str__(self):
        return self.stu_name


# 课程表
class course(models.Model):
    co_num = models.AutoField(primary_key=True)  # 定义一个id自增的字段，并设置为主键
    co_name = models.CharField(max_length=32)
    co_teacher = models.CharField(max_length=32)

    class Meta:
        verbose_name_plural = '课程表'

    def __str__(self):
        return self.co_name


# 选课表
class chco(models.Model):
    chco_num = models.AutoField(primary_key=True)
    # 设置外键，当主键表中删除一条记录，外键表中修改也相应删除
    chco_st = models.ForeignKey(student, on_delete=models.CASCADE)
    chco_co = models.ForeignKey(course, on_delete=models.CASCADE)

    class Meta:
        verbose_name_plural = '选课表'


# 学期表
class term(models.Model):
    # 定义一个id自增的字段，并设置为主键
    te_num = models.AutoField(primary_key=True)
    te_name = models.CharField(max_length=32)

    class Meta:
        verbose_name_plural = '学期表'

    def __str__(self):
        return self.te_name


# 成绩表
class mark(models.Model):
    ma_num = models.AutoField(primary_key=True)
    # 设置外键，当主键表中删除一条记录，外键表中改行也相应删除
    ma_st = models.ForeignKey(student, on_delete=models.CASCADE)
    ma_xueqi = models.ForeignKey(term, on_delete=models.CASCADE)
    ma_mark = models.IntegerField()

    class Meta:
        verbose_name_plural = '成绩表'


# 消费表
class money(models.Model):
    mo_num = models.AutoField(primary_key=True)
    mo_st = models.ForeignKey(student, on_delete=models.CASCADE)
    mo_month = models.IntegerField()  # 定义整型字段
    mo_money = models.IntegerField()

    class Meta:
        verbose_name_plural = '消费表'


# 书籍表
class book(models.Model):
    bo_num = models.AutoField(primary_key=True)
    bo_name = models.CharField(max_length=32)

    class Meta:
        verbose_name_plural = '书籍表'

    def __str__(self):
        return self.bo_name


# 借书表
class lend(models.Model):
    le_num = models.AutoField(primary_key=True)
    le_st = models.ForeignKey(student, on_delete=models.CASCADE)
    le_bo = models.ForeignKey(book, on_delete=models.CASCADE)
    le_start = models.DateTimeField()
    le_end = models.DateTimeField(auto_now=True)  # 自动更新时间

    class Meta:
        verbose_name_plural = '借书表'
